package com.heima.wemedia.controller;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.dtos.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 自媒体素材表(WmMaterial)表控制层
 *
 * @author makejava
 * @since 2022-04-10 17:12:49
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
    /**
     * 服务对象
     */
    @Resource
    private WmMaterialService wmMaterialService;

    /**
     * 分页查询
     *
     * @param wmMaterial  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<WmMaterial>> queryByPage(WmMaterial wmMaterial, PageRequest pageRequest) {
        return ResponseEntity.ok(this.wmMaterialService.queryByPage(wmMaterial, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<WmMaterial> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.wmMaterialService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmMaterial 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<WmMaterial> add(WmMaterial wmMaterial) {
        return ResponseEntity.ok(this.wmMaterialService.insert(wmMaterial));
    }

    /**
     * 编辑数据
     *
     * @param wmMaterial 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<WmMaterial> edit(WmMaterial wmMaterial) {
        return ResponseEntity.ok(this.wmMaterialService.update(wmMaterial));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.wmMaterialService.deleteById(id));
    }


    /*
    图片上传
    /api/v1/material/upload_picture
     */
    @PostMapping("upload_picture")
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {

        return wmMaterialService.upload(multipartFile);
    }

/*
/api/v1/material/list

分页查询
 */

    @PostMapping("list")
    public ResponseResult list(@RequestBody WmMaterialDto wmMaterialDto) throws IOException {

        return wmMaterialService.list(wmMaterialDto);
    }

    /*
    wemedia/api/v1/material/collect/1
    收藏
     */
    @GetMapping("collect/{id}")
    public ResponseResult collect(@PathVariable Integer id  ) throws IOException {

        return wmMaterialService.collect(id);
    }
    /*
    /api/v1/material/cancel_collect/{id}
    取消收藏
     */
    @GetMapping("cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable Integer id  ) throws IOException {

        return wmMaterialService.cancelCollect(id);
    }

    ///api/v1/material/del_picture/{id}

    @GetMapping("del_picture/{id}")
    public ResponseResult del(@PathVariable Integer id  ) throws IOException {

        return wmMaterialService.del(id);
    }

}

